import { createRouter, createWebHashHistory, RouteLocationNormalized, RouteRecordRaw,} from 'vue-router';
import ROUTER_CONSTANTS from './constants';
import { clearPending } from '../utils/Axious';

// 注册页面
const RegisterPage = () => import('../views/RegisterView.vue');
// 登录页面
const LoginPage = () => import('../views/LoginView.vue');
// 后台管理系统根页面
const BasePage = () => import('../views/BaseView.vue');
// 职位发布页面
const PublishPage = () => import('../views/PublishView.vue');
// 职位列表页面
const JobListPage = () => import('../views/JobListView.vue');

// 路由关系映射表
const routes: RouteRecordRaw[] = [
  // 注册页面
  {
    path: '/register',
    name: ROUTER_CONSTANTS.REGISTER,
    component: RegisterPage,
  },
  // 登录页面
  {
    path: '/login',
    name: ROUTER_CONSTANTS.LOGIN,
    component: LoginPage,
  },
  // 后台管理系统页面
  {
    path: '/',
    name: 'Home',
    component: BasePage,
    redirect: '/joblist',
    children: [
      {
        path: '/publish',
        name: ROUTER_CONSTANTS.PUBLISH,
        component: PublishPage,
      },
      {
        path: '/joblist',
        name: ROUTER_CONSTANTS.JOBLIST,
        component: JobListPage,
      },
    ],
  },
];

// 创建 Hash 模式路由对象
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 全局导航守卫
router.beforeEach(
  (to: RouteLocationNormalized, from: RouteLocationNormalized) => {
    // 清除未完成请求
    clearPending();
    return true;
  }
);

// 导出路由对象
export default router;
